// pages/map/washCar.js
var QQMapWX = require('../../utils/qqmap.js');
const app = getApp();
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    location: {},
    markers: [],
    height: 400
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    qqmapsdk = new QQMapWX({
      key: app.globalData.qqmapsdk // 必填
    });
    this.setData({
      height: app.globalData.sysInfo.windowHeight
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userLocation']) {
          wx.showModal({
            title: '信息提示',
            content: '未授权定位，请先授权',
            success: res => {
              if (res.confirm) {
                wx.navigateTo({
                  url: 'auth',
                });
              } else if (res.cancel) {
                wx.navigateBack({})
              }
            }
          });
        } else {
          wx.getLocation({
            success: res => {
              this.setData({
                location: res
              });
              qqmapsdk.search({
                keyword: '洗车美容',
                location: res,
                page_size: 20,
                success: res => {
                  var mks = []
                  for (var i = 0; i < res.data.length; i++) {
                    mks.push({ // 获取返回结果，放到mks数组中
                      title: res.data[i].title,
                      id: i,
                      latitude: res.data[i].location.lat,
                      longitude: res.data[i].location.lng,
                      address: res.data[i].address,
                      iconPath: "/icons/mark.png", //图标路径
                      width: '40rpx',
                      height: '40rpx',
                      callout: {
                        content: res.data[i].title,
                        color: '#fdb434',
                        fontSize: '26rpx',
                        bgColor: 'transparent'
                      }
                    })
                  }
                  this.setData({ //设置markers属性，将搜索结果显示在地图中
                    markers: mks
                  })
                }
              })
            },
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  callouttap: function(e) {
    var index = e.detail.markerId;
    var markers = this.data.markers;
    var item = markers[index];
    wx.showModal({
      title: '提示信息',
      content: '是否需要导航到' + item.title,
      success: res => {
        if (res.confirm) {
          //启用地图导航
          wx.openLocation({
            latitude: item.latitude,
            longitude: item.longitude,
            name: item.title,
            address: item.address,
            scale: 14
          })
        }
      }
    })
  }
})